﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Collections.Generic;

namespace HaoLab.Library
{
    public class Utility
    {
        public static string ASCII2Unicode(string ASCII)
        {
            Regex reg = new Regex(@"&#x(\w*);");
            var m = reg.Matches(ASCII);
            Debug.WriteLine(m.Count);

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < m.Count; i++)
            {
                byte[] bb = new byte[2];
                bb[1] = Convert.ToByte(m[i].Groups[1].Value.Substring(0, 2), 16);
                bb[0] = Convert.ToByte(m[i].Groups[1].Value.Substring(2, 2), 16);
                sb.Append(Encoding.Unicode.GetString(bb, 0, 2));
            }

            Debug.WriteLine(sb.ToString());
            return sb.ToString();
        }

    }

    public class HttpWebClient : WebClient
    {
        private CookieContainer cookies;
        [SecuritySafeCritical]
        public HttpWebClient()
        {
            this.cookies = new CookieContainer();
        }

        public HttpWebClient(CookieContainer cookies)
        {
            this.cookies = cookies;
        }

        public CookieContainer Cookies
        {
            get
            {
                return this.cookies;
            }
            set
            {
                this.cookies = value;
            }
        }

        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);

            if (request is HttpWebRequest)
            {
                HttpWebRequest httpRequest = request as HttpWebRequest;
                httpRequest.CookieContainer = cookies;
            }
            return request;
        }
    }

    public static class HaoExtension
    {
        /// <summary>

        /// Sends an HTML form. The request is sent using POST method

        /// </summary>

        /// <param name="webclient">The webclient.</param>

        /// <param name="uri">The URI of the resource to receive the file. </param>

        /// <param name="data">The form data to upload.</param>

        public static void PostHtmlForm(this WebClient webclient, Uri uri, IEnumerable<KeyValuePair<string, string>> data)
        {

            StringBuilder dataBuilder = new StringBuilder();

            int cnt = 0;

            foreach (var item in data)
            {

                if (cnt++ > 0)
                {

                    dataBuilder.Append('&');

                }

                dataBuilder.Append(item.Key);

                dataBuilder.Append('=');

                dataBuilder.Append(HttpUtility.UrlEncode(item.Value));

            }

            webclient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

            webclient.UploadStringAsync(uri, dataBuilder.ToString());

        }



        /// <summary>

        /// Sends an HTML form. The request is sent using POST method

        /// </summary>

        /// <param name="webclient">The webclient.</param>

        /// <param name="uri">The URI of the resource to receive the file. </param>

        /// <param name="data">The form data to upload.</param>

        /// <param name="userToken">user state</param>

        public static void PostHtmlForm(this WebClient webclient, Uri uri, IEnumerable<KeyValuePair<string, string>> data, object userToken)
        {

            StringBuilder dataBuilder = new StringBuilder();

            int cnt = 0;

            foreach (var item in data)
            {

                if (cnt++ > 0)
                {

                    dataBuilder.Append('&');

                }

                dataBuilder.Append(item.Key);

                dataBuilder.Append('=');

                dataBuilder.Append(HttpUtility.UrlEncode(item.Value));

            }


            webclient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

            webclient.UploadStringAsync(uri, "POST", dataBuilder.ToString(), userToken);

        }

        public static void GetHtmlForm(this WebClient webclient, Uri uri, IEnumerable<KeyValuePair<string, string>> data)
        {


            StringBuilder dataBuilder = new StringBuilder();

            int cnt = 0;

            foreach (var item in data)
            {

                if (cnt++ > 0)
                    dataBuilder.Append('&');
                else
                    dataBuilder.Append('?');

                dataBuilder.Append(item.Key);

                dataBuilder.Append('=');

                dataBuilder.Append(HttpUtility.UrlEncode(item.Value));

            }
            uri = new Uri(uri.ToString() + dataBuilder.ToString());
            webclient.DownloadStringAsync(uri);
        }
    }
}
